const CryptoJS = require("crypto-js");
import token from "../api/token"

function getUrl() {
  if (location.hostname.indexOf("gitee") != -1) {
    return "https://39.107.221.29/api"
  }
  return (location.protocol + "//" + location.hostname) + (location.hostname == "39.107.221.29" ? "/api" : ":9025")
  //码云上直接写死"https://39.107.221.29/api"
}

function param2(url, param) {
  url += "?"
  for (let i in param) {
    url += i + "=" + param[i] + "&"
  }
  return getUrl() + url
  //http://39.107.221.29:3000
}


function myget(url, param) {
  param.timer = token.getTimer()
  // console.log(param)
  return fetch(param2(url, { ...param, uToken: token.getToken() }), {
    method: "GET",
    headers: {
      "uToken": token.getToken(),
    },
  }).then((res) => {
    return res.json()
  })
}

function mypost(url, param, body) {
  param.timer = token.getTimer()
  return fetch(param2(url, { ...param, uToken: token.getToken() }), {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(body)
  }).then((res) => {
    return res.json()
  })
}

function myupload(url, param, body) {
  param.timer = token.getTimer()
  return fetch(param2(url, param), {
    method: "POST",
    headers: {
      'Accept': 'application/json',
    },
    body: JSON.stringify(body)
  }).then((res) => {
    return res.json()
  })
}


// function mye100(url,body) {
//   body.method="POST"
//   body.mode= "no-cors"
//   body.headers={
//          Host: 'www.e100soft.com',
//          'Proxy-Connection': 'keep-alive',
//          'Content-Length': '1542756',
//          'Cache-Control': 'max-age=0',
//          'Upgrade-Insecure-Requests': '1',
//          Origin: 'http://127.0.0.1:8080',
//          'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary5ADIauiqXScF3yGp',
//          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.49',
//          Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
//          Referer:' http://127.0.0.1:8080/',
//          'Accept-Encoding': 'gzip, deflate',
//          'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',

//       }
//   return fetch(url, body).then((res) => {
//     console.log(5,res.text())
//     return res.text()
//   })
// }
export default {
  myget,
  mypost,
  myupload,
  getUrl
}
